/*******************************************************************************
 * Copyright @ 2017 YunSpace.Framework ��Ȩ����
 * Author: YunSpace
 * Description: YunSpace���ٿ���ƽ̨
 * Website��http://www.YunSpace.cn
*********************************************************************************/

using System;
using MySql.Data.MySqlClient;
using System.Data.Common;
using YunSpace.Core;
using YunSpace.Data;
using YunSpace.Domain.Entity.SystemManage;
using YunSpace.Domain.IRepository.SystemManage;
using YunSpace.Repository.SystemManage;

namespace YunSpace.Application.SystemManage
{
    public class UserLogOnApp
    {
        private IUserLogOnRepository service = new UserLogOnRepository();

        public UserLogOnEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }

        public void UpdateForm(UserLogOnEntity userLogOnEntity)
        {
            service.Update(userLogOnEntity);
        }

        public void RevisePassword(string userPassword, string keyValue)
        {
            UserLogOnEntity userLogOnEntity = new UserLogOnEntity();
            userLogOnEntity.F_Id = keyValue;
            userLogOnEntity.F_UserSecretkey = Md5.md5(DateTime.Now.ToString("yyyyMMddHHmmssfff"), 16).ToLower();
            userLogOnEntity.F_UserPassword = Md5.md5(DESEncrypt.Encrypt(userPassword.ToLower(), userLogOnEntity.F_UserSecretkey).ToLower(), 32).ToLower();

            // service.Update(userLogOnEntity);
            using (var db = new RepositoryBase().BeginTrans())
            {
                var strSql = @"update Sys_UserLogOn set F_UserSecretkey=@F_UserSecretkey, F_UserPassword=@F_UserPassword where F_Id=@F_Id";
                DbParameter[] parameter =
                {
                    new MySqlParameter("@F_UserSecretkey", userLogOnEntity.F_UserSecretkey),
                    new MySqlParameter("@F_UserPassword", userLogOnEntity.F_UserPassword),
                    new MySqlParameter("@F_Id", userLogOnEntity.F_Id),
                };
                db.ExecuteSql(strSql,parameter);
                db.Commit();
            }
        }

    }
}

